#include "p_range_set.h"

P_Range_Set::P_Range_Set(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    P_Rang_N = Sysset->Gas[GasSet_ABC-1].gasValve;
    timer2 = new QTimer(this);
    timer2->setInterval(300);
    ui_Label_Init();
    ui_Btn_Init();
   connect_Init();
   time_Init();
   Sysctrl->SYS_CTL_MODE = 0;
}
void P_Range_Set::connect_Init()
{

    //确定
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
        if(change_flag) //切换至按键控制模式
               {
                   change_flag = 0;
                   timer2->stop();
                   theLabel[1]->setText(QString::number(number[P_Rang_N]));
                   Sysset->Phot_Range[0]=P_Rang_N;
              }
               else  if(!change_flag) //切换至数据修改模式
               {
                   change_flag = 1;
                   timer2->start();
               }

    });

    //返回
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
    });

}

void P_Range_Set::time_Init()
{
    //定时器
    connect(timer2,&QTimer::timeout,[=](){
        if(show_flag)
        {
            theLabel[1]->setText((QString::number(number[P_Rang_N])));
            show_flag =0 ;
        }
        else
        {
            str_show="";
            for(int i=0;i<5;i++)
            {
                    str_show.append("  ");
            }
            theLabel[1]->setText(str_show);
            show_flag=1;
        }
        qDebug()<<Sysset->Gas_Num;
    });
}
void P_Range_Set::reciveDensity()
{
    P_Rang_N++;
    if(P_Rang_N >=0&&P_Rang_N <9)
    {
        switch (P_Rang_N)
        {
        case 0:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 1:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 2:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 3:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 4:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 5:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 6:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("当前为自定义量程")+" "+QString::number(P_Rang_N-5));
            break;
        }
        case 7:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("当前为自定义量程")+" "+QString::number(P_Rang_N-5));
            break;
        }
        case 8:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("当前为自定义量程")+" "+QString::number(P_Rang_N-5));
            break;
        }
        default:
            break;
        }
    }
    else
    {
        P_Rang_N=0;
        theLabel[1]->setText(QString::number(number[P_Rang_N]));
        theLabel[5]->setText(tr("")+" ");
    }
}
void P_Range_Set::reciveDensity2() //减的时候的函数
{
    P_Rang_N--;
    if(P_Rang_N >=0&&P_Rang_N <9)
    {
        switch (P_Rang_N)
        {
        case 0:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 1:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 2:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 3:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 4:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 5:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("")+" ");
            break;
        }
        case 6:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("当前为自定义量程")+" "+QString::number(P_Rang_N-5));
            break;
        }
        case 7:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("当前为自定义量程")+" "+QString::number(P_Rang_N-5));
            break;
        }
        case 8:
        {
            theLabel[1]->setText(QString::number(number[P_Rang_N]));
            theLabel[5]->setText(tr("当前为自定义量程")+" "+QString::number(P_Rang_N-5));
            break;
        }
        default:
            break;
        }
    }
    else
    {
        P_Rang_N=8;
        theLabel[1]->setText(QString::number(number[P_Rang_N]));
        theLabel[5]->setText(tr("当前为自定义量程")+" "+QString::number(P_Rang_N-5));

    }
}


bool P_Range_Set::eventFilter(QObject *watched, QEvent *event)
{

//            key_Event::key_Event1(pos,watched, event, btnlist, btnlist_ui);
//          for(int i=0;i<btnlist_ui.length();i++)
            if(btnlist_ui.at(0) == watched||btnlist_ui.at(1) == watched)
        {
            if(event->type() == QEvent::KeyPress)
            {
                QKeyEvent *ev = (QKeyEvent *)event;
                QPushButton *button = (QPushButton *)watched;

               {
                   switch (ev->key())
                   {
                   case OK:
                   {

                       emit  button-> clicked();
                     break;
                   }
                   case UP: //Up
                     {
                       if(change_flag)
                       {
                           reciveDensity2();
                       }
                       else
                       focusPreviousChild();
                       break;
                     }
                   case DOWN: //Down
                     {
                       if(change_flag)
                       {
                          reciveDensity();
                       }
                       else
                      focusNextChild();
                       break;
                     }
                   case BACK:
                   {
                       this->parentWidget()->show();
                       this->close();
                       break;
                   }
                   case INIT:
                   {
                       init_Show *show = new init_Show(this);
                       show->show();
                       this->hide();

                       break;
                   }
                    default:
                       break;
                   }
               }
            }
            }
            return QWidget::eventFilter(watched,event);//返回父类的事件过滤器

}

void P_Range_Set::ui_Label_Init()
{

    for(int i=0;i<6;i++)//创建界面显示的label
    {
         theLabel[i] = new QLabel(this);
         theLabel[i]->setStyleSheet("QLabel{\
                                outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                                border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                border-style: outset;\
                                /* 按钮文本设置 */\
                                qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                font-size: 20px;\
                                color: #123567;			                 /* 文本颜色 */\
                           }"
          );
    }
theLabel[0]->setGeometry(50,150,200,40);//设定每隔放置一个按键
theLabel[1]->setGeometry(300,150,100,40);//设定每隔放置一个按键
theLabel[5]->setGeometry(200,250,200,40);//设定每隔放置一个按键
theLabel[5]->setStyleSheet("QLabel{"
                      + the_StyleSheet[20]+ " font-size: 22px;"+ "background-color: rgb(0, 0, 0);	    /* 背景颜色 */"+  "}"    );


    theLabel[0]->setText(tr("当前选择的量程："));
    theLabel[1]->setText(QString::number(number[P_Rang_N]));


    theLabel[2]->setStyleSheet("QLabel{"
                          + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
    theLabel[3]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
    theLabel[4]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );
    theLabel[5]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

   theLabel[2]->setGeometry(0,0,640,60);//设定每隔放置一个按键
   theLabel[3]->setGeometry(0,0,300,60);//设定每隔放置一个按键
   theLabel[4]->setGeometry(340,0,300,60);//设定每隔放置一个按键
   theLabel[2]->setText("选择自定义量程");


}


void P_Range_Set::ui_Btn_Init()
{

    for(int i=0;i<2;i++)//创建主屏幕显示的按键
    {
         theBtn[i] = new QPushButton(this);
}
btnlist_ui.append(theBtn[0]);
btnlist_ui.append(theBtn[1]);
theBtn[0]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/切换.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/切换2.png); /* 背景图片 */"+"}");
theBtn[1]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");

btnlist_ui.at(0)->setGeometry(150,300,120,90 );
btnlist_ui.at(1)->setGeometry(360,300,120,90 );
btnlist_ui.at(0)->setText(tr("确定"));
btnlist_ui.at(1)->setText(tr("返回"));

//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
    btn->installEventFilter(this);
}
}
